package com.cloud.workTest;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MappingJsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

public class JacksonTest {


    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        JsonFactory f = new MappingJsonFactory();
        JsonParser jp = f.createParser(new File("F:/csvjson.json"));
        JsonToken current;
        current = jp.nextToken();

        if (current != JsonToken.START_ARRAY) {
            System.out.println("Error: root should be array: quiting.");
            return;
        }

        Integer count = 0;

        while (jp.nextToken() != JsonToken.END_ARRAY) {
            // read the record into a tree model,
            // this moves the parsing position to the end of it
            JsonNode node = jp.readValueAsTree();
            Iterator<String> iterator = node.fieldNames();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }
//            DataSource datasource = objectMapper.treeToValue(node, DataSource.class);
//            System.out.println(datasource);
        }
        jp.close();

        System.out.println(count);
    }
}
